package com.gezi.admin.module.app.file;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.gezi.admin.config.application.ApplicationUtil;
import com.gezi.admin.config.exception.type.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author 格子代码
 * @createDate 2024/2/10 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/api/app/file")
public class FileUpController {

    @ApiOperation("上传一个文件")
    @PostMapping(value = "/up")
    public Object up(@RequestParam("file") MultipartFile file, String rootPath) throws IOException {
        if(!file.isEmpty()){
            return saveFile(file,rootPath);
        }else{
            throw ApiException.getInstance("上传失败");
        }
    }

    @ApiOperation("上传多个文件")
    @PostMapping(value = "/ups")
    public Object ups(@RequestParam("files") MultipartFile[] files,String rootPath) throws IOException {
        if(files.length > 0){
            List<Map<String,String>> list = ListUtil.toList();
            for (MultipartFile multipartFile : files) {
                list.add(saveFile(multipartFile,rootPath));
            }
            return list;
        }
        throw ApiException.getInstance("上传失败");
    }

    /**
     * 保存文件到本地
     * @param multipartFile
     * @param rootPath
     * @return
     * @throws IOException
     */
    private Map<String,String> saveFile(MultipartFile multipartFile, String rootPath) throws IOException {
        rootPath = rootPath.replaceAll("\\\\","/");
        if(!rootPath.startsWith("/")){
            rootPath = "/" + rootPath;
        }
        String fileName = multipartFile.getOriginalFilename();

        //新文件名
        String newFileName = String.format("%s.%s", IdUtil.simpleUUID(), FileUtil.getSuffix(fileName));

        //相对路径和绝对路径
        String newRelativePath = "";
        if(StrUtil.isNotBlank(rootPath)){
            newRelativePath = String.format("%s/%s", rootPath, newFileName);
        }
        String realPath = ApplicationUtil.fileLocalPath + newRelativePath;

        //创建文件夹
        cn.hutool.core.io.FileUtil.mkParentDirs(realPath);
        //保存文件到本地
        multipartFile.transferTo(FileUtil.file(realPath));

        Map<String,String> result = MapUtil.newHashMap();
        result.put("fileName",fileName);//文件原名
        result.put("type",fileName.substring(fileName.lastIndexOf(".")+1));//文件类型
        result.put("name",newFileName);//文件名
        result.put("url",ApplicationUtil.fileWebUrl + newRelativePath);//相对路径

        return result;
    }
}